package GUI;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import javax.swing.JViewport;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JTabbedPane;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;

import Client.HTSMain;

import javax.swing.ImageIcon;

public class FullRun extends Menu {
	
	public static String StepID;
	private JTextArea ActualtextArea = null; 
	private int col; 
	private int row;
	public ArrayList<Object> fullTable;
	public static ArrayList<Object> Step;
	private JTextArea CommentTextArea= null; 
	private JTable table= null;
	
	public FullRun(ArrayList<Object> msg) {
		
		fullTable =Run.str; 
		Step = msg;
		//row= Integer.parseInt(str[2]);
		initComponents(Step,fullTable);	
	}

	
	public void initComponents(final ArrayList<Object> str, final ArrayList<Object> fullTable)
	{
		Menu.btnRun.setEnabled(false); //setting buttons
		Menu.StopButton.setEnabled(true);
		Menu.btnAddScen.setEnabled(false);
		Menu.btnAddUser.setEnabled(false);
		Menu.btnDeleteScen.setEnabled(false);
		Menu.btnHome.setEnabled(false);
		Menu.btnStatistic.setEnabled(false);
		Menu.btnUpdatescenario.setEnabled(false);
		Menu.btnScenarioBank.setEnabled(false);
		Menu.btnExit.setEnabled(false);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(56, 472, 401, 195);
		add(scrollPane_1);
	
		JTextArea ExpectedtextArea = new JTextArea((String)Step.get(4));
		ExpectedtextArea.setFont(new Font("Courier New", Font.PLAIN, 14));
		scrollPane_1.setViewportView(ExpectedtextArea);
		ExpectedtextArea.setEditable(false);
		ExpectedtextArea.setWrapStyleWord(true);
		ExpectedtextArea.setLineWrap(true);
		
		JLabel lblExpected = new JLabel("Expected:");
		lblExpected.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblExpected.setBounds(56, 447, 76, 24);
		add(lblExpected);
		
		JScrollPane scrollPane_2 = new JScrollPane();
		scrollPane_2.setBounds(504, 472, 401, 195);
		add(scrollPane_2);
		
		ActualtextArea = new JTextArea((String)Step.get(5));
		ActualtextArea.setFont(new Font("Courier New", Font.PLAIN, 14));
		scrollPane_2.setViewportView(ActualtextArea);
		ActualtextArea.setWrapStyleWord(true);
		ActualtextArea.setLineWrap(true);
		
		JLabel lblActual = new JLabel("Actual:");
		lblActual.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblActual.setBounds(504, 451, 62, 17);
		add(lblActual);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setFont(new Font("Tahoma", Font.PLAIN, 14));
		tabbedPane.setBounds(56, 277, 850, 163);
		add(tabbedPane);
		
		JTextArea TestStepTextArea = new JTextArea((String)Step.get(3));
		TestStepTextArea.setFont(new Font("Courier New", Font.PLAIN, 14));
		TestStepTextArea.setEditable(false);
		TestStepTextArea.setWrapStyleWord(true);
		TestStepTextArea.setLineWrap(true);
		tabbedPane.addTab("Test Step", null, TestStepTextArea, null);
		
		CommentTextArea = new JTextArea((String)Step.get(6));
		CommentTextArea.setFont(new Font("Courier New", Font.PLAIN, 14));
		tabbedPane.addTab("Comment", null, CommentTextArea, null);
		CommentTextArea.setWrapStyleWord(true);
		CommentTextArea.setLineWrap(true);
		
		
		
		
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(56, 103, 850, 163);
		add(scrollPane);

		Vector<Vector<Object>> data = new Vector<Vector<Object>>();
		 

        for (int i=3; i<=(this.fullTable.size()-3);i=i+3){
        Vector<Object> row = creatrow(this.fullTable,i);
        data.add(row);
        }
	
        Vector<String> headers = new Vector<String>();
        headers.add("Step ID");
        headers.add("Description");
        headers.add("Status");
        table = new JTable( data, headers );
        table.setFont(new Font("Tahoma", Font.PLAIN, 14));
        table.addMouseListener(new MouseAdapter() {
        	@Override
        	public void mousePressed(MouseEvent arg0) {
        		
        		 col = table.getSelectedColumn();   
                 row = table.getSelectedRow();
                boolean is=table.isColumnSelected(0);
                
                if (!is){
        			
        			JOptionPane.showMessageDialog(null, "Please select Step ID only","Error",JOptionPane.ERROR_MESSAGE);

        		}
                else // mouse listener
                {
                	//Menu.btnRun.setEnabled(true);                           
                	
                	StepID=((String)table.getValueAt(row, col));
            		
                	ArrayList AddArray=new ArrayList();
    	 			AddArray.add("LoadStep");
    	 			AddArray.add(StepID);
    	 			AddArray.add(str.get(1));
    	 			
    	 			HTSMain.chat.handleMessageFromGui(AddArray);
            		
                }
        		
        	}
        });
       // table.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        table.setShowHorizontalLines(true);
        table.setShowVerticalLines(true);
        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
        centerRenderer.setHorizontalAlignment( JLabel.CENTER );
        JTableHeader header = table.getTableHeader();
        header.setDefaultRenderer(centerRenderer);
        for (int i=0;i<table.getColumnCount();i++)
        {
        	table.getColumnModel().getColumn(i).setCellRenderer( centerRenderer );
        	
        }
        table.setRowHeight(20);
        table.setPreferredScrollableViewportSize(new Dimension(900,150));
        scrollPane.setViewportView(table);
       // table.setSelectionMode(row);
        JViewport viewport = (JViewport)table.getParent();
        Rectangle rect = table.getCellRect(Integer.parseInt((String) str.get(2))-1, 0, true);
        Point pt = viewport.getViewPosition();
        rect.setLocation(rect.x-pt.x, rect.y-pt.y);
        viewport.scrollRectToVisible(rect);
        
        JLabel lblNewLabel = new JLabel(""); /// X
        lblNewLabel.addMouseListener(new MouseAdapter() {
        	@Override
        	public void mousePressed(MouseEvent arg0) {
        		
        		if (StepID==null)
        		{
        			JOptionPane.showMessageDialog(null, "Please select Step ID only","Error",JOptionPane.ERROR_MESSAGE);
        		}
        		else
        		{
        		int Num =Integer.parseInt((String)Step.get(2));
        		ArrayList AddArray=new ArrayList();
	 			AddArray.add("FailStep");
	 			AddArray.add((String)Step.get(2));
	 			AddArray.add(str.get(1));
	 			AddArray.add(ActualtextArea.getText());
	 			AddArray.add(CommentTextArea.getText());
	 			Run.str.set(Num*3, "Fail");
	 			//Run.str[Num*3]= "Fail";
	 			HTSMain.chat.handleMessageFromGui(AddArray);
        		}
        		
        		
        		
        		
        		
        	}
        });
        lblNewLabel.setIcon(new ImageIcon(Run.class.getResource("/Images/delete_48.png")));
        lblNewLabel.setBounds(931, 609, 62, 58);
        add(lblNewLabel);
        
        JLabel lblNewLabel_1 = new JLabel(""); ///V
        lblNewLabel_1.addMouseListener(new MouseAdapter() {
        	@Override
        	public void mousePressed(MouseEvent arg0) {
        		
        		if ((String)Step.get(2)==null)
        		{
        			JOptionPane.showMessageDialog(null, "Please select Step ID only","Error",JOptionPane.ERROR_MESSAGE);        
  
        		}
        		else
        		{
        		int Num =Integer.parseInt((String)Step.get(2));
        		ArrayList AddArray=new ArrayList();
	 			AddArray.add("PassStep");
	 			AddArray.add((String)Step.get(2));
	 			AddArray.add(str.get(1));
	 			AddArray.add(ActualtextArea.getText());
	 			AddArray.add(CommentTextArea.getText());
	 			Run.str.set(Num*3, "Pass");
	 			HTSMain.chat.handleMessageFromGui(AddArray);
        		}
        		
        		
        		
        	}
        });
        lblNewLabel_1.setIcon(new ImageIcon(Run.class.getResource("/Images/vb.png")));
        lblNewLabel_1.setBounds(928, 447, 76, 92);
        add(lblNewLabel_1);
        
        JLabel lblQm = new JLabel("");
        lblQm.addMouseListener(new MouseAdapter() {
        	@Override
        	public void mousePressed(MouseEvent arg0) {
        		
        		
        		if (StepID==null)
        		{
        			JOptionPane.showMessageDialog(null, "Please select Step ID only","Error",JOptionPane.ERROR_MESSAGE);        
  
        		}
        		else
        		{
        		int Num =Integer.parseInt((String)Step.get(2));
        		ArrayList AddArray=new ArrayList();
	 			AddArray.add("UnknowStep");
	 			AddArray.add((String)Step.get(2));
	 			AddArray.add(str.get(1));
	 			AddArray.add(ActualtextArea.getText());
	 			AddArray.add(CommentTextArea.getText());
	 			Run.str.set(Num*3, "Unknow");
	 			//Run.str[Num*3]= "Unknow";
	 			HTSMain.chat.handleMessageFromGui(AddArray);
        		}
        		
        		
        	}
        });
        lblQm.setIcon(new ImageIcon(FullRun.class.getResource("/Images/qm2.png")));
        lblQm.setBounds(931, 538, 66, 49);
        add(lblQm);
        
        
		
	} 
	
	
	private Vector<Object> creatrow(ArrayList<Object> msg, int i) {
		 Vector<Object> row = new Vector<Object>();
    row.add( ""+msg.get(i));
    row.add( ""+msg.get(i+1));
    row.add( ""+msg.get(i+2));

		return row;
	}
}
